/**
 * @class yijs.zuosa
 * @fileOverview 做啥网网页客户端
 * @author 谢文浩
 * @email mohaiguyan12@126.com
 * @version 0.1
 * @date 2010-01-23
 * Copyright (c) 2010-2010 谢文浩
 */
/**命名空间*/
var yijs = yijs || {};
/**
 * 继承函数，使用原型继承
 * @param {Object} subClass    子类
 * @param {Object} superClass  父类
 */
yijs.extend = function(subClass,superClass){
	var F = function(){};
	F.prototype = superClass.prototype;
	subClass.prototype = new F();
	subClass.prototype.constructor = subClass;
	subClass.superClass = superClass.prototype;
	if(superClass.prototype.constructor == Object.prototype.constructor){
		superClass.prototype.constructor = superClass;
	}
}
yijs.setTpl = function(tpl,arr){
	if(tpl && arr){
		$.each(arr,function(i){
			var n = "("+i+")";
			tpl = tpl.replace(n,arr[i]);
		})
		
	}
	return tpl;
}
yijs.zuosa = {};

yijs.zuosa.TYPE = ["json","xml","rss","atom"];

yijs.zuosa.API = "http://api.zuosa.com/";
yijs.zuosa.APIURI = ['search','statuses','users','friendships','direct_messages','account','tag','register','help','favorites','blocks'];
yijs.zuosa.ACTION = {
		/**搜索*/
		search : "search",
		/**显示大家在做啥*/
		publicMsg : "public_timeline",
		/**显示用户在做啥*/
		userMsg : "user_timeline",
		/**显示用户和好友在做啥*/
		friendsMsg : "friends_timeline",
		/**获取一条指定的消息*/
		getMsg : "show",
		/**发布一条新消息*/
		updateMsg : "update",
		/**删除一条消息*/
		delMsg : "destroy",
		/**获取一条指定的消息*/
		getMentions : "mentions"		
}
/**
 * 获取完整的API路径
 */
yijs.zuosa.getFullApiPath = function(uri,action){
	return yijs.zuosa.API + uri + "/" + action+"."+yijs.zuosa.TYPE[0];
}
/**
 * 格式化时间
 */
yijs.zuosa.formatTime = function(t){
	var values = t.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value)-1000*60*60*8; //we use +0800 timezone.
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	if(delta < 60){
		return "少于 1 分钟";
	}else if(delta < 120){
		return "约 1 分钟前";
	}else if(delta < (45*60)){
		return (parseInt(delta / 60)).toString() + " 分钟前";
	}else if(delta < (90*60)){
		return "约 1 小时前";
	}else if(delta < (24*60*60)){
		return "约 " + (parseInt(delta / 3600)).toString() + " 小时前";
	}else if(delta < (48*60*60)){
		return "1 天前";
	}else{
		return (parseInt(delta / 86400)).toString() + " 天前";
	}			
}
yijs.zuosa.Msg = function(options){
	var defaults = yijs.zuosa.Msg.defaultOptions;
	//覆盖默认参数
	this.options = $.extend(defaults, options);	
	this.listeners = this.options.listeners;
	this.$applyTo = null;
	if(this.options.autoRender) this.render();		
}
//默认参数
yijs.zuosa.Msg.defaultOptions = {
		autoRender : false,
		renderTo : null,
		applyTo : null,
		id : null,
		api : null,
		width : 350,
		height : 400,
		autoHeight : false,
		style : null,
		isScroll : true,
		scrollDelay : 3000,
		speed : "fast",
		count : 10,
		iconWidth : 50,
		iconHeight : 50,
		isShowIcon : true,
		isUseItemAnimate : true,
		fnCallBack : null,
		listeners : {
			render : null
		}
}
yijs.zuosa.Msg.cls = {
		applyTo : "yijs_zuosa_msg",
		item : "yijs_zuosa_msg_item",
		itemEven : "yijs_zuosa_msg_item_even"
}
yijs.zuosa.Msg.tpl = {
		header : "<div class='yijs_zuosa_msg_header'>(0)在做啥</div>",
		ownerIcon : "<div class='yijs_zuosa_msg_ownerIcon'>(0)</div>",
		item : "<div class='yijs_zuosa_msg_item_icon'>(0)</div>" +
			   "<div class='yijs_zuosa_msg_item_details'>" +
			   		"<div class='yijs_zuosa_msg_item_header'>" +
			   			"<span class='yijs_zuosa_msg_item_name'>(1)</span>&nbsp;&nbsp;" +
			   			"<span class='yijs_zuosa_msg_item_created'>(3)</span>&nbsp;&nbsp;通过" +	
			   			"<span class='yijs_zuosa_msg_item_source'>(4)</span>" +		   			
			   		"</div>" +
			   		"<div class='yijs_zuosa_msg_item_text'>(2)</div>" +
			   "</div>"
		
}

yijs.zuosa.Msg.prototype = {
		render : function(){
			var _opts = this.options;
			var _cls = yijs.zuosa.Msg.cls;
			if(_opts.renderTo != null){
				$("<ul />",{"class" : _cls.applyTo}).appendTo(_opts.renderTo);
				this.$applyTo = $(_opts.renderTo).children("ul");
			}
			if(_opts.applyTo != null){
				$(_opts.applyTo).addClass(_cls.applyTo);
				this.$applyTo = $(_opts.applyTo);
			}
			if(this.$applyTo != null){
				this._setApplyToStyle();
				this.send();
			}
		},
		send : function(){
			var _that = this;
			if(this.options.api != null){
				/*
				$.getJSON(this.options.api + "&callback=?",function(data){
					_that._create.call(this,_that,data);
				})
				*/
				$.ajax({
					   type: "POST",
					   url: this.options.api,
					   data: {},
					   dataType : "json",
					   success: function(data){
						   _that._create.call(this,_that,data);
					   }
				}); 
			}
		},
		callback : function(_that,data){
			if(_that.options.fnCallBack == null){
				_that._create(_that,data);
			}else{
				if(typeof _that.options.fnCallBack == "function"){
					_that.options.fnCallBack.call(_that,data);
				}
			}
		},
		_create : function(_that,data){
			var _cls = yijs.zuosa.Msg.cls;
			$.each(data,function(i,item){
				var _user = item.user;
				var _icon = "<img src='"+_user.profile_image_url+"' width='"+_that.options.iconWidth+"' height='"+_that.options.iconHeight+"' />";
				var _name = _user.name;
				var _text = item.text;
				var _time = yijs.zuosa.formatTime(item.created_at);
				var _source = item.source;
				var _html = yijs.setTpl(yijs.zuosa.Msg.tpl.item,[_icon,_name,_text,_time,_source]);
				$("<li />",{"class":_cls.item,"id":item.id}).appendTo(_that.$applyTo).html(_html);
			})
			_that._setItemStyle();
		},
		_setItemStyle : function(){
			var _that = this;
			var _cls = yijs.zuosa.Msg.cls;
			var $li = this.$applyTo.children("li:even");
			if($li.size() > 0){
				$li.addClass(_cls.itemEven);
			}
			_that.options.isUseItemAnimate == true && this._itemShowAnimate();
			
		},
		_itemShowAnimate : function(){
			var $li = this.$applyTo.children("li"); 
			var len = $li.size();
			_animate(0);	
			function _animate(eq){
				$li.eq(eq).fadeIn("slow",function(){
					if(eq < len-1){
						  eq ++;
						  _animate(eq);						
					}
				})
			}			
		},
		_setApplyToStyle : function(){
			var _opts = this.options;
			_opts.id != null && this.$applyTo.attr("id",_opts.id);
			_opts.style != null && this.$applyTo.css(_opts.style);
			this.$applyTo.width(_opts.width);
			_opts.autoHeight = false && this.$applyTo.height(_opts.height);			
		}
		
}

yijs.zuosa.UserMsg = function(options){
	var defaults = yijs.zuosa.Msg.defaultOptions;
	defaults.userName = null;
	//覆盖默认参数
	this.options = $.extend(defaults, options);	
	this.listeners = this.options.listeners;
	//this.options.api = yijs.zuosa.getFullApiPath(yijs.zuosa.APIURI[1],yijs.zuosa.ACTION.userMsg)+"?id="+this.options.userName;
	this.options.api = "json/test.js";
	if(this.options.autoRender) this.render();		
}
yijs.extend(yijs.zuosa.UserMsg,yijs.zuosa.Msg);


